import 'package:fluro/fluro.dart';
import 'package:flutter/widgets.dart';
import 'package:imperius_insights/portal/env/environments.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart';

enum CompactHostMode { Inside, Outside }

Future navigateTo(BuildContext context, String path,
    {Map<String, String> params,
    TransitionType transition = TransitionType.cupertino,
    CompactHostMode hostMode = CompactHostMode.Inside}) {
  if (isWebRuntime && hostMode == CompactHostMode.Outside) {
    var pathWithParams = path;
    for (MapEntry<String, String> pair in params.entries) {
      pathWithParams = pathWithParams.replaceFirst(':${pair.key}', pair.value);
    }
    return openBrowserOutside('#/$pathWithParams');
  }
  final router = Provider.of<Router>(context, listen: false);
  if (params == null) {
    return router.navigateTo(context, path, transition: transition);
  }
  var pathWithParams = path;
  for (MapEntry<String, String> pair in params.entries) {
    pathWithParams = pathWithParams.replaceFirst(':${pair.key}', pair.value);
  }
  return router.navigateTo(context, pathWithParams, transition: transition);
}

Future<bool> openBrowserOutside(String url) {
  return launch(url);
}
